import java.util.Stack;

class Solution227 {
    int index = 0 ; 
    public int calculate(String s) {
        char ret = '+' ; 
        char[] arr = s.toCharArray() ;
        Stack<Integer> stack = new Stack<>() ;
        while(index < arr.length){
           char ch = arr[index] ; 
           if(ch >= '0' && ch <= '9'){
              int num = getNum(arr) ; 
              if(ret == '+'){
                stack.push(num) ; 
              }else if(ret == '-'){
                stack.push(-num) ; 
              }else if(ret == '*'){
                 int prev = stack.pop() ;
                  stack.push(prev * num) ; 
              }else{
                 int prev = stack.pop() ;
                  stack.push(prev / num) ; 
              }
           }else if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
               ret = ch ;
               index ++ ;
           }else{
            index ++ ;
           }
        }
       int sum = 0 ; 
       while(!stack.empty()){
        sum += stack.pop() ;
       }
       return sum ;
    }
    private int getNum(char[] arr){ 
        StringBuilder strbu = new StringBuilder() ;
        while(index < arr.length && arr[index] >='0' && arr[index] <= '9'){
            strbu.append(arr[index++]);
        }
        return Integer.parseInt(strbu.toString()) ; 
    }
}